package com.yanqu.road.dao.impl.channelcenter.washe;

import com.yanqu.road.dao.cache.SqlCacheParam;
import com.yanqu.road.dao.impl.TempNoPoolCacheDao;
import com.yanqu.road.entity.activity.washe.config.WasheConfig;
import com.yanqu.road.entity.activity.washe.config.WasheRoundConfig;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class TempWasheRoundConfigDaoImpl extends TempNoPoolCacheDao {

    public TempWasheRoundConfigDaoImpl(String dbParam) {
        super(dbParam);
    }

    private static String getWasheRoundConfigSql = "select * from `t_n_washepvp_round` where `activityId`=? and ( `seasonId`=? or `seasonId`=? )";

    public Map<Integer, WasheRoundConfig> getWasheRoundConfigMap(int activityId, int seasonId, int channelActivityTimeZone) {
        SqlCacheParam sqlCacheParam = getCacheParam(getWasheRoundConfigSql);
        sqlCacheParam.addParam(Integer.class, activityId);
        sqlCacheParam.addParam(Integer.class, WasheConfig.SEASON_ID_NULL);
        sqlCacheParam.addParam(Integer.class, seasonId);
        sqlCacheParam.addOtherParam(channelActivityTimeZone);
        // 先取缓存
        Object cacheResult = getCacheResult(sqlCacheParam);
        if (cacheResult != null) {
            return (Map<Integer, WasheRoundConfig>) cacheResult;
        }
        Map<Integer, WasheRoundConfig> map = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            call = createCall(sqlCacheParam);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                WasheRoundConfig washeRoundConfig = new WasheRoundConfig();
                washeRoundConfig.setActivityId(rs.getInt("activityId"));
                washeRoundConfig.setSeasonId(rs.getInt("seasonId"));
                washeRoundConfig.setRound(rs.getInt("round"));
                washeRoundConfig.setSceneSkillId(rs.getInt("sceneSkillId"));
                washeRoundConfig.setWarTimeStr(rs.getString("warTime"));
                washeRoundConfig.setConfigPreparationTime(rs.getInt("preparationTime"));
                washeRoundConfig.init(channelActivityTimeZone);

                map.put(washeRoundConfig.getRound(), washeRoundConfig);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            closeConnection(call, rs);
        }
        // 添加缓存
        addCacheResult(sqlCacheParam, map);
        return map;
    }

}
